package com.qu.waibao.network;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.qu.waibao.AppConfig;
import com.qu.waibao.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import info.wangchen.simplehud.SimpleHUD;

/**
 * @描述 describe
 * @作者 Administrator
 * @创建时间 2018/8/2 11:03
 * @邮箱 1123599561@qq.com
 */
public class HttpModeBase {

    // Ctrl+Shift+X小写转大写

    /* 请求ID */
    public static final int HTTP_ERROR = 0x00101;
    public static final int HTTP_SUCCESS= 0x00100;
    public static final int HTTP_REQUESTCODE_1 = 0x00101;
    public static final int HTTP_REQUESTCODE_2 = 0x00102;
    public static final int HTTP_REQUESTCODE_3 = 0x00103;
    public static final int HTTP_REQUESTCODE_4 = 0x00104;
    public static final int HTTP_REQUESTCODE_5 = 0x00105;
    public static final int HTTP_REQUESTCODE_COMMENT = 0x00200;
    public static final int HTTP_REQUESTCODE_ANSWER = 0x00201;
    public static final int HTTP_REQUESTCODE_DIANZAN = 0x00202;
    public static final int HTTP_REQUESTCODE_SHARE = 0x00203;
    public static final int HTTP_REQUESTCODE_DEL = 0x00204;
    public static final int HTTP_REQUESTCODE_SENDMESSAGE = 0x00205;
    public static final int HTTP_REQUESTCODE_LOGIN = 0x00206;
    public static final int HTTP_FINISH = 0x00199;
    //Activity请求码
    public static final int ACTIVITY_REQUESTCODE = 0X01000;
    //Activity结果码
    public static final int ACTIVITY_RESULTCODE = 0X02000;

    private Handler mHandler;
    private Context mContext;
    private Message msg;
    private Bundle bundle = new Bundle();
    private int mTypeId;

    public HttpModeBase(Handler mHandler, Context mContext) {
        super();
        this.mHandler = mHandler;
        this.mContext = mContext;
        this.msg = mHandler.obtainMessage();
    }

    /**
     * xutilsPost请求 异步的
     *
     * @param url
     * @param filePath
     * @return
     */
    public void xutilsPost(String url, final JSONObject json, String filePath, final Boolean isProgress) {
        if (isProgress) {
            SimpleHUD.showLoadingMessage(mContext, "上传图片中...", AppConfig.isCanCancel);
        }
        String stat = null;
        try {
            stat = URLEncoder.encode(json.toString(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        RequestParams params = new RequestParams(url + stat);
        params.addHeader("Content-Type", "application/json");
        params.setConnectTimeout(30 * 1000);
        File file = new File(filePath);
        if (file != null && file.exists()) {
            params.addBodyParameter("image", file);
            params.setMultipart(true);
            Callback.Cancelable cancelable = x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            Message msg = mHandler.obtainMessage();
                            msg.obj = result;
                            msg.what = HTTP_SUCCESS;
                            mHandler.sendMessage(msg);
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Message msg = new Message();
                            msg.what = HTTP_ERROR;
                            msg.obj = "网络请求有误";
                            mHandler.sendMessage(msg);
                            ex.printStackTrace();// 把错误信息打印出轨迹来
                            SimpleHUD.dismiss();
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                            Message msg = new Message();
                            msg.what = HTTP_ERROR;
                            msg.obj = "上传请求已取消";
                            mHandler.sendMessage(msg);
                            SimpleHUD.dismiss();
                        }

                        @Override
                        public void onFinished() {
                            SimpleHUD.dismiss();
                        }
                    }
            );
        } else {
            ToastUtil.show(mContext,"图片路径不存在");
        }
    }


    /**
     * xutilsPostList 请求异步的
     * @param url
     * @param filePath
     * @return
     */
    public void xutilsPostList(String url, final JSONObject json, String filePath, final int position, final Boolean isProgress) {

        if (!TextUtils.isEmpty(filePath)) {
            String stat = null;
            try {
                stat = URLEncoder.encode(json.toString(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            RequestParams params = new RequestParams(url + stat);
            params.addHeader("Content-Type", "application/json");
            params.setConnectTimeout(30 * 1000);
            File file = new File(filePath);
            if (file != null && file.exists()) {
                params.addBodyParameter("image", file);
                params.setMultipart(true);
                Callback.Cancelable cancelable = x.http().post(params, new Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                Message msg = mHandler.obtainMessage();
                                msg.what = HTTP_SUCCESS;
                                msg.arg1 = 1;//1为上传返回
                                msg.arg2 = position;
                                msg.obj = result;
                                mHandler.sendMessage(msg);
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                Message msg = new Message();
                                msg.what = HTTP_ERROR;
                                msg.obj = "网络请求有误";
                                msg.arg1 = 1;//1为上传返回
                                msg.arg2 = position;
                                mHandler.sendMessage(msg);
                                ex.printStackTrace();// 把错误信息打印出轨迹来
                            }

                            @Override
                            public void onCancelled(CancelledException cex) {
                                Message msg = new Message();
                                msg.what = HTTP_ERROR;
                                msg.arg1 = 1;//1为上传返回
                                msg.arg2 = position;
                                msg.obj = "上传请求已取消";
                                mHandler.sendMessage(msg);
                            }

                            @Override
                            public void onFinished() {

                            }
                        }
                );
            } else {

                ToastUtil.show(mContext,"图片路径不存在");
            }
        } else {
            Message msg = mHandler.obtainMessage();
            msg.arg2 = position;
            if(filePath.contains("hbyc_file/")){
                msg.arg1 = 1;
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("status",1);
                    jsonObject.put("result",filePath);
                    jsonObject.put("message","");

                    msg.obj = jsonObject.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                msg.arg1 = 0;//0为不用上传
                msg.obj = filePath;
            }
            mHandler.sendMessage(msg);
        }
    }

}
